/* 
 * jobs/form.js
 */


$(document).ready(function(){

  $('#JobVersion0JobTypeId').change(handleJobTypeChange);
  $('#JobVersion0ParamProjApplyGaussianfilter').click(handleGaussianFilterCheckboxClick);
  $('#submitToCluster').change(handleSubmitToClusterChange);
  
  // Initialize the Accordion for JobGroup dir/Job dir seletion widget
  $("#jobGroupSelector").accordion({
			collapsible: true,
      autoHeight: false, // allow each accordian panel to set its own content height (rather than forcing all panels to share the largest height)
      active: '#jobGroupSelector #open_panel'  // the content panel to display on load
	});
  // Initialize the Accordion for "Add Job"/"Add Job Version"
  $("#accordianWrapper").accordion({
			collapsible: false,
      autoHeight: false // allow each accordian panel to set its own content height (rather than forcing all panels to share the largest height)
      //active: '#jobGroupSelector #open_panel'  // the content panel to display on load
	});
  // Handle radio btn click on Job dir selection widget
  $('.jobNameRadios').click(handleJobNameRadioClick);

  // Give the Fiji input data launch button a data value for the projection indir on hover
  // (this way it has the needed data right before the onClick handler does the Ajax call)
  $('#fijiInputDataLaunchBtn').mouseenter(function(){
    $(this).attr('data-proj_indir', $('#JobVersion0ParamProjIndir').val());
  });
  // When only adding a new Version, provide the ability to show/hide Job details
  var jobDetailsToggleCount = 1;
  $('#showJobDetailsLink').click(function(){
    if(jobDetailsToggleCount % 2){
      $('#jobDetails').show(300);
      $('#showJobDetailsLink').html('hide details');
    }
    else {
      $('#jobDetails').hide(300);
      $('#showJobDetailsLink').html('show details');
    }
    jobDetailsToggleCount++;
  });
  
}); // end document ready func



// Handle radio btn click on Job dir selection widget
function handleJobNameRadioClick() {
  
  //alert("You clicked a job name radio. NAME: " + $(this).val());
  
  // The context of "$(this)" here is the radio btn input that was clicked
  
  $('#JobName').val($(this).val());
      //$('#JobJobGroup').val($(this).attr('data-jobgroup'));
  // This fills both the disabled input and the hidden input
  $('.JobJobGroupDiv input').val($(this).attr('data-jobgroup'));
      //$('#JobDirName').val($(this).val());    
  // This fills both the disabled input and the hidden input
  $('.JobDirNameDiv input').val($(this).val());
  // Fill the hidden job group id input
  $('#JobJobGroupId').val($(this).attr('data-jobgroupid'));
  
      // TODO: ? somehow replace "/" with a portable directory-separator? (i.e., for windows)
      //$('#JobDirAbsPath').val( $(this).attr('data-jobgroupfullpath') + "/" + $(this).val() );
      //$('.JobDirAbsPathDiv input').val( $(this).attr('data-jobgroupfullpath') + "/" + $(this).val() );
  
  $('.secondaryJobStuff').show(700, null);
  $('.submit input[type="submit"]').attr('disabled', false)
                                   .removeClass("disabledSubmit")
                                   .siblings(".tooltipSpan").css('display', 'none');
                                   
  // Add data to the Launch Fiji btn that makes image stacks from the input projection data
  var groupId    = $('#JobJobGroupId').val();
  var jobDirName = $('#JobDirName').val();
  $('#fijiInputDataLaunchBtn').attr('data-group_id',     groupId);
  $('#fijiInputDataLaunchBtn').attr('data-job_dir_name', jobDirName);
  
} //////////// end handleJobNameRadioClick() //////////////


jobTypeEnum = {
  // Each item (FDK, PSIRT, etc) is referred to as a 'jobTypeObj' in some cases below
  // The 'value' property corresponds to the 'value' in the HTML element, as well as the id
  //  of the db record in the job_types table
  FDK :      {value: 1, fieldsetId: '#paramFdkFieldset'},
  PSIRT:     {value: 2, fieldsetId: '#paramSirtFieldset'}, // Shares a fieldset with SIRT
  SIRT:      {value: 3, fieldsetId: '#paramSirtFieldset'},
  RECON_NONE:{value: 4, fieldsetId: '#paramReconNoneFieldset'},
  NONE_SELECTED: {value: 0, fieldsetId: '#'}
}


function handleJobTypeChange() {

  showSpeed = 400;
  

  switch(parseInt($(this).val())) {
    
    case jobTypeEnum.FDK.value :
      
      handleShowingJobTypeFieldset(jobTypeEnum.FDK, showSpeed);
      break;
      
    case jobTypeEnum.PSIRT.value :
      
      handleShowingJobTypeFieldset(jobTypeEnum.PSIRT, showSpeed);
      // Select the appropriate value for PSIRT inside the Sirt Params fieldset (sirt_type, either PSirt or Sirt)
      selectorStr = jobTypeEnum.PSIRT.fieldsetId + ' #ParamSirtBasisSirtTypeId';
      $(selectorStr).val('1'); // the value associated with PSIRT
      break;
      
    case jobTypeEnum.SIRT.value :

      handleShowingJobTypeFieldset(jobTypeEnum.SIRT, showSpeed);
      // Select the appropriate value for SIRT
      selectorStr = jobTypeEnum.SIRT.fieldsetId + ' #ParamSirtBasisSirtTypeId';
      $(selectorStr).val('2'); // the value associated with SIRT
      break;
    
    case jobTypeEnum.RECON_NONE.value :

      handleShowingJobTypeFieldset(jobTypeEnum.RECON_NONE, showSpeed);
      break;
      
    default:
      handleShowingJobTypeFieldset(jobTypeEnum.NONE_SELECTED, showSpeed);
  } /// end switch($(this).val())
  
} //////////// end func handleJobTypeChange() //////


function handleShowingJobTypeFieldset(jobTypeObj, showSpeed) {
  
  hideOtherJobTypes(jobTypeObj, showSpeed);
  if(jobTypeObj.value != 0) {
    $(jobTypeObj.fieldsetId).show(showSpeed, null);
  }
  
} ////////// end handleShowingJobTypeFieldset() ///////////

/**
 * 
 */
function hideOtherJobTypes(keepJobTypeObj, hideSpeed) {
  //hideSpeed = 400;
  
  for(var jobType in jobTypeEnum ) {
    
    //if(jobTypeEnum[jobType].value != keepJobTypeValue)   {
    if(jobTypeEnum[jobType].fieldsetId != keepJobTypeObj.fieldsetId)   {
      // hide it
      $(jobTypeEnum[jobType].fieldsetId).hide(hideSpeed, null);
    }
  }
} //////////// end hideOtherJobTypes ///////////


function handleGaussianFilterCheckboxClick() {
  if(this.checked) {
    $('#gaussianSigmaDiv').show(400);
  }
  else {
    $('#gaussianSigmaDiv').hide(400);
    $('#ParamProjGaussSigmaGaussSigma').val(''); 
  }
} ///////////// end func ////////////////



function handleSubmitToClusterChange() {
  
  showSpeed = 400;
  
  if(this.checked) {
    $('#sendEmailDiv').show(showSpeed);
    $('#cudaWallTimeDiv').show(showSpeed);
  }
  else {
    $('#sendEmailDiv').hide(showSpeed);
    $('#cudaWallTimeDiv').hide(showSpeed);
    //$('#ParamProjGaussSigmaGaussSigma').val(''); 
  }
  
} //////////////// end handleSubmitToClusterChange() ///////////////